package com.situ.mybatis.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	static SqlSessionFactory factory = null;
		
		static {
			String resource = "mybatis.xml";
			InputStream inputStream;
			try {
				inputStream = Resources.getResourceAsStream(resource);
				// 创建SqlSessionFactory
				factory = new SqlSessionFactoryBuilder().build(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public static SqlSession getSqlSession() {
			// 创建SqlSession
			SqlSession sqlSession = factory.openSession();
			return sqlSession;
		}
}
